package com.ywq.app.biz.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ywq.app.biz.ArtRoomBiz;
import com.ywq.app.biz.IdentifierBiz;
import com.ywq.app.dao.ArtRoomDao;
import com.ywq.app.dao.ArtistDao;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtRoom;
import com.ywq.app.domain.ArtRoomArtist;
import com.ywq.app.domain.ArtRoomPic;
import com.ywq.app.domain.ArtRoomVideo;
import com.ywq.common.constant.PageSize;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.IdGenerator;

public class ArtRoomBizImpl implements ArtRoomBiz {

	private final static Logger logger = LoggerFactory
			.getLogger(ArtRoomBizImpl.class);
	@Autowired
	private IdentifierBiz idBiz;
	
	private ArtRoomDao artRoomDao;
	private ArtistDao artistDao;

	public void setArtistDao(ArtistDao artistDao) {
		this.artistDao = artistDao;
	}

	public void setArtRoomDao(ArtRoomDao artRoomDao) {
		this.artRoomDao = artRoomDao;
	}

	// 展览馆列表
	@Override
	public PageModel getArtRoomList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_ADMIN);
		map.put("pageSize", PageSize.PAGE_SIZE_ADMIN);
		int count = artRoomDao.getArtRoomListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artRoomDao.getArtRoomList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_ADMIN);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_ADMIN - 1)
				/ PageSize.PAGE_SIZE_ADMIN);
		return pm;
	}

	// 添加展览馆
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public void insertArtRoom(ArtRoom artRoom,List<ArtArtist> artistList) throws AppBusinessException {
		//String id = IdGenerator.createIdentifier();
		String id = idBiz.generateId("art_room");
		artRoom.setIdentifier(id);
		if("".equals(artRoom.getStartTime())|| "".equals(artRoom.getEndTime())){
			artRoom.setStartTime("0");
			artRoom.setEndTime("0");
		}
		artRoomDao.insertArtRoom(artRoom);
		for(ArtArtist artArtist :artistList){
			String istId = IdGenerator.createIdentifier();
			artArtist.setId(istId);
			artArtist.setType(PageSize.ARTIST_ROOM);
			//增加画家
			artistDao.insertArtArtist(artArtist);
			//增加画家与展览馆关联表
			ArtRoomArtist artRoomArtist =new ArtRoomArtist();
			artRoomArtist.setArtistId(istId);
			artRoomArtist.setRoomId(id);
			artRoomDao.insertArtRoomIst(artRoomArtist);
		}
	}

	// 更新展览馆
	@Override
	public void updateArtRoom(ArtRoom artRoom) throws AppBusinessException {
		artRoomDao.updateArtRoom(artRoom);
	}
	// 更新展览馆
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public void updateRoom(ArtRoom artRoom,List<ArtArtist> artistList) throws AppBusinessException {
		artRoomDao.deleteRoomArtist(artRoom.getIdentifier());
		if("".equals(artRoom.getStartTime())|| "".equals(artRoom.getEndTime())){
			artRoom.setStartTime("0");
			artRoom.setEndTime("0");
		}
		artRoomDao.updateArtRoom(artRoom);
		for(ArtArtist artArtist :artistList){
			String istId = IdGenerator.createIdentifier();
			artArtist.setId(istId);
			artArtist.setType(PageSize.ARTIST_ROOM);
			//增加画家
			artistDao.insertArtArtist(artArtist);
			//增加画家与展览馆关联表
			ArtRoomArtist artRoomArtist =new ArtRoomArtist();
			artRoomArtist.setArtistId(istId);
			artRoomArtist.setRoomId(artRoom.getIdentifier());
			artRoomDao.insertArtRoomIst(artRoomArtist);
		}
	}

	// 前台查询展览馆当前
	@Override
	public List<ArtRoom> getNowRoomList() throws AppBusinessException {
		List<ArtRoom> nowList = null;
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("isShow", "true");
			nowList = artRoomDao.getNowRoomList(map);
			if (nowList.size() > 0) {
				nowList=formatTime(nowList);
			}
		return nowList;
	}
	//
	
	
	// 前台查询展览馆old
	@Override
	public List<ArtRoom> getOldRoomList() throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", "true");
		return artRoomDao.getOldRoomList(map);

	}

	// 按个数查询历史
	@Override
	public List<ArtRoom> getOldRoomListBySize(Integer size)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", "true");
		map.put("pageSize", size);
		map.put("startId", 0);
		return artRoomDao.getOldRoomList(map);

	}

	// 历史分页

	@Override
	public PageModel getRBackList(Integer page,Integer y) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_ART_OLD);
		map.put("pageSize", PageSize.PAGE_ART_OLD);
		map.put("isShow", "true");
		map.put("year", y);
		int count = artRoomDao.getOldRoomListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtRoom> oldList =artRoomDao.getOldRoomList(map);
		oldList=formatTime(oldList);
		pm.setList(oldList);
		pm.setPageSize(PageSize.PAGE_ART_OLD);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_ART_OLD - 1)
				/ PageSize.PAGE_ART_OLD);
		return pm;
	}

	// 格式化时间
	@Override
	public List<ArtRoom> formatTime(List<ArtRoom> rList) throws AppBusinessException {
		try {
			for (ArtRoom art : rList) {
				SimpleDateFormat secData = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat secTime = new SimpleDateFormat("HH:mm:ss");
				Date sDate = secData.parse(art.getStartDate());
				Date eDate = secData.parse(art.getEndDate());
				if("0".equals(art.getStartTime())|| "0".equals(art.getEndTime())){
					
				}else{
					Date sTime = secTime.parse(art.getStartTime());
					Date eTime = secTime.parse(art.getEndTime());
					art.setsTime(sTime);
					art.seteTime(eTime);
				}
				art.setsDate(sDate);
				art.seteDate(eDate);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return rList;
	}
	//展览馆详情
	@Override
	public ArtRoom getInRoomById(String id)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		map.put("isShow", "true");
		ArtRoom artRoom =artRoomDao.getInRoomById(map);
		//增加关联的作品
		Map<String, Object> mapPic = new HashMap<String, Object>();
		mapPic.put("roomId", id);
		mapPic.put("isShow", "true");
		mapPic.put("pageSize", 3);
		mapPic.put("startId", 0);
		List<ArtRoomPic> artPicList =artRoomDao.getArtRoomPicById(mapPic);
		artRoom.setArtPicList(artPicList);
		return artRoom;
		
	}
	//展览馆详情不含作品
	@Override
	public ArtRoom getRoomById(String id)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		ArtRoom artRoom =artRoomDao.getInRoomById(map);
		return artRoom;
		
	}
	//根展览馆得到画家
	@Override
	public List<ArtArtist> getArtArtistList(String roomId) throws AppBusinessException {
			List<ArtArtist> artList =artRoomDao.getArtArtistList(roomId);
		return artList;
	}
	//增加展览馆关联图片
	public void insertArtRoomPic(ArtRoomPic artRoomPic)throws AppBusinessException {
		artRoomDao.insertArtRoomPic(artRoomPic);
	}
	//
	@Override
	public PageModel getModelRpics(String roomId,Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "room");
		map.put("roomId", roomId);
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_PIC);
		map.put("pageSize", PageSize.PAGE_SIZE_PIC);
		int count = artRoomDao.getArtRoomPicByIdCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtRoomPic> artPicList =artRoomDao.getArtRoomPicById(map);
		pm.setList(artPicList);
		pm.setPageSize(PageSize.PAGE_SIZE_PIC);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_PIC - 1)/ PageSize.PAGE_SIZE_PIC);
		return pm;
	}
	//一个一页
	@Override
	public PageModel getModelRpicsOne(String roomId,Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "room");
		map.put("roomId", roomId);
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_PICPIC);
		map.put("pageSize", PageSize.PAGE_SIZE_PICPIC);
		int count = artRoomDao.getArtRoomPicByIdCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtRoomPic> artPicList =artRoomDao.getArtRoomPicById(map);
		pm.setList(artPicList);
		pm.setPageSize(PageSize.PAGE_SIZE_PICPIC);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_PICPIC - 1)/ PageSize.PAGE_SIZE_PICPIC);
		return pm;
	}
	// 展览馆列表For
		@Override
		public PageModel getArtRoomListForMisc(Integer page,Integer pageSize) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
			int count = artRoomDao.getArtRoomListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artRoomDao.getArtRoomList(map));
			pm.setPageSize(pageSize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pageSize- 1)
					/ pageSize);
			return pm;
		}
		@Override
		public PageModel getRoomPicListGrid(String roomId,Integer page,Integer pagesize) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("type", "room");
			map.put("roomId", roomId);
			map.put("startId", (page - 1) * pagesize);
			map.put("pageSize", pagesize);
			int count = artRoomDao.getArtRoomPicByIdCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			List<ArtRoomPic> artPicList =artRoomDao.getArtRoomPicById(map);
			pm.setList(artPicList);
			pm.setPageSize(pagesize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pagesize - 1)/ pagesize);
			return pm;
		}
		@Override
		public List<ArtRoomPic>  getArtRoomListByRId(String roomId)throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("type", "room");
			map.put("roomId", roomId);
			return	artRoomDao.getArtRoomPicById(map);
		}
		@Override
		public void deleteRoomPic(String picId,String roomId)throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("picId", picId);
			map.put("roomId", roomId);
			artRoomDao.deleteRoomPic(map);
		}
		@Override
		public void deleteRoom(String roomId)throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("roomId", roomId);
			artRoomDao.deleteRoom(map);
		}
		//保存视频
		@Override
		public void insertArtFlv(ArtRoomVideo artRoomVideo) throws AppBusinessException {
			artRoomDao.insertArtFlv(artRoomVideo);
		}
		//
		@Override
		public PageModel getArtRoomFlv(Integer page,Integer pagesize) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pagesize);
			map.put("pageSize", pagesize);
			int count = artRoomDao.getArtRoomFlvCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			List<ArtRoomVideo> artPicList =artRoomDao.getArtRoomFlv(map);
			pm.setList(artPicList);
			pm.setPageSize(pagesize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pagesize - 1)/ pagesize);
			return pm;
		}
		@Override
		public void deleteFlv(String id)throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", id);
			artRoomDao.deleteFlv(map);
		}
		@Override
		public List<ArtRoomVideo> getArtRoomVideoByRoomId(String roomId) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", 0);
			map.put("pageSize", 3);
			map.put("roomId", roomId);
			return artRoomDao.getArtRoomFlv(map);
		}
		@Override
		public List<ArtRoomVideo> getAllArtRoomVideoByRoomId(String roomId) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("roomId", roomId);
			return artRoomDao.getArtRoomFlv(map);
		}
}
